const { usersModel } = require("../models/usersModel");

const { examTypeModel } = require("../models/examTypeModel");
const { knowledgePointsModel } = require("../models/knowledgePointsModel");
const {
    questionsModel
} = require("../models/questionsModel");
const {
    testsModel
} = require("../models/testsModel");
const {
    completedQuestionsModel
} = require("../models/completedQuestionsModel");
const {
    completedTestModel
} = require("../models/completedTestModel");

const { errorQuestionsModel } = require("../models/errorQuestionsModel");
//将做完的试卷添加到完成试卷
//将做完的题目添加到完成的题目
async function addcompletedTest(req, res, next) {
    // 前端要传用户每道题的答案
    let {
        score,
        test_id,
        user_id,
        userAnswerArr
    } = req.body;

    userAnswerArr = JSON.parse(userAnswerArr);
    //添加
    const result = await completedTestModel.create({
        score,
        test_id,
        user_id
    });


    // //添加完成的问题
    let status = false;
    let key;
    for (let i = 0; i < userAnswerArr.length; i++) {
        let qId = userAnswerArr[i]._id;
        let arr = await completedQuestionsModel.find({ questions_id: { _id: qId }, user_id }).populate('questions_id').populate('test_id').populate('user_id');

        if (userAnswerArr[i].selectVal == "A") {
            key = 0
        } else if (userAnswerArr[i].selectVal == "B") {
            key = 1;
        } else if (userAnswerArr[i].selectVal == "C") {
            key = 2;
        } else {
            key = 3;
        }
        if (arr.length == 0) {

            let resultQues = await completedQuestionsModel.create({
                questions_id: userAnswerArr[i]._id,
                test_id,
                user_id,
                user_answer: key
            });
            if (resultQues) {
                status = true;
            }
        }

    }

    if (result && status) {
        res.send({
            result,
            message: '添加成功',
            code: 1
        });
    } else {
        res.send({
            message: '添加失败',
            code: 0
        });
    }
}


// //添加错题

async function addErrorTest(req, res, next) {
    // 前端要传用户每道题的答案
    let {
        user_id,
        userAnswerArr
    } = req.body;

    userAnswerArr = JSON.parse(userAnswerArr);

    let status = false;
    let key;
    for (let i = 0; i < userAnswerArr.length; i++) {
        let qId = userAnswerArr[i]._id;
        let arr = await errorQuestionsModel.find({ errorQuestions_id: { _id: qId }, user_id }).populate('errorQuestions_id').populate('user_id');

        if (userAnswerArr[i].selectVal == "A") {
            key = 0
        } else if (userAnswerArr[i].selectVal == "B") {
            key = 1;
        } else if (userAnswerArr[i].selectVal == "C") {
            key = 2;
        } else {
            key = 3;
        }
        if (arr.length == 0) {

            let resultQues = await errorQuestionsModel.create({
                ErrorAnswer: key,
                errorQuestions_id: userAnswerArr[i]._id,
                user_id

            });
            if (resultQues) {
                status = true;
            }
        }

    }

    if (status) {
        res.send({
            message: '添加成功',
            code: 1
        });
    } else {
        res.send({
            message: '添加失败',
            code: 0
        });
    }
}


//查看解析
async function viewResolution(req, res, next) {
    const {
        user_id,
        test_id
    } = req.body;

    //添加
    const  completedTest = await completedTestModel.find({
        test_id,
        user_id
    }).populate("test_id").populate("user_id");

    //用户答案
    let completedQue = await completedQuestionsModel.find({ user_id, test_id }).populate({
        path: 'test_id',
        populate: {
            path: "TypeId"
        }
    }).populate({
        path: 'questions_id',
        populate: {
            path: "KnowledgePointId"
        }
    }).populate('user_id');

    res.send({
        completedTest,
        completedQue

    });

}
module.exports = {
    addcompletedTest,
    viewResolution,
    addErrorTest
}